package com.lxl.hadoop.order;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lixilin
 * @Date 2022/1/1 11:18
 * @Description
 */
public class OrderReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {

    Map<Integer, Integer> orderCount = new HashMap<Integer, Integer>();
    NullWritable nullWritable = NullWritable.get();
    @Override
    protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
//       context.write(key,nullWritable);
        Integer count = orderCount.get(key.getOrderCode());
        count = count == null ? 0:count;
        if (count < 2) {
            context.write(key, nullWritable);
            orderCount.put(key.getOrderCode(), ++count);
        }
    }
}
